﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ProcmonPivot
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		public MainWindow()
		{
			InitializeComponent();
			this.Loaded += MainWindow_Loaded;
		}

		void MainWindow_Loaded(object sender, RoutedEventArgs e)
		{
			//Dispatcher.BeginInvoke(new Action(() =>
			//{
			//	ProcmonPivotOffice.Create("Logfile.csv");
			//}));
		}

		private void RunProcmon(object sender, RoutedEventArgs e)
		{
			string procmon = Preferences.Default.ProcmonPath;
			if(String.IsNullOrEmpty(procmon) || !File.Exists(procmon))
			{
				OpenFileDialog open = new OpenFileDialog();
				open.Filter = "Executable|*.exe";
				var result = open.ShowDialog();
				if(result != null && result.Value)
				{
					procmon = open.FileName;
				}
				else
					return;
			}
			Process.Start(procmon);
			Preferences.Default.ProcmonPath = procmon;
			Preferences.Default.Save();
		}

		private void OpenTrace(object sender, RoutedEventArgs e)
		{
			OpenFileDialog dialog = new OpenFileDialog();
			dialog.InitialDirectory = ThisPath;
			var result = dialog.ShowDialog();
			if(result != null && result.Value)
			{
				ProcmonPivotOffice.Create(dialog.FileName);
			}
		}

		public string ThisPath
		{
			get
			{
				return new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName;
			}
		}

		private void ClearPreferences(object sender, RoutedEventArgs e)
		{
			Preferences.Default.Reset();
			Preferences.Default.Save();
		}

		private void OpenPreferences(object sender, RoutedEventArgs e)
		{
			new PreferencesWindow().ShowDialog();
		}
	}
}
